<template>
    <a-modal v-model:visible="visible" :width="1000" title="选择" @ok="handleOk" :footer="null">
        <a-form class="smart-query-form">
            <a-row class="smart-query-form-row">


                <a-form-item label="登录账号" class="smart-query-form-item">
                    <a-input style="width: 200px" v-model:value="queryForm.username" placeholder="请输入角色名称" />
                </a-form-item>

                <a-form-item class="smart-query-form-item smart-margin-left10">
                    <a-button-group>
                        <a-button type="primary" @click="onSearch">
                            <template #icon>
                                <SearchOutlined />
                            </template>
                            查询
                        </a-button>
                        <a-button @click="resetQuery">
                            <template #icon>
                                <ReloadOutlined />
                            </template>
                            重置
                        </a-button>

                    </a-button-group>
                    <a-button type="primary" @click="tableSelectChange" style="margin-left: 10px;">
                        确定
                    </a-button>
                </a-form-item>
            </a-row>
        </a-form>
        <a-table size="small" rowKey="id" :dataSource="tableData" :columns="columns" :pagination="false"
            :loading="tableLoading" bordered
            :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange,type:'radio' }">
            <template #bodyCell="{ column, record, text, index }">
                <template v-if="column.dataIndex === 'index'">
                    {{ index+1 }}
                </template>
                <template v-if="column.dataIndex === 'gender'">
                    {{ record.gender===1?'女':'男' }}
                </template>
            </template>
        </a-table>

        <div class="smart-query-table-page">
            <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS"
                :defaultPageSize="queryForm.pageSize" v-model:current="queryForm.pageNumber"
                v-model:pageSize="queryForm.pageSize" :total="total" @change="ajaxQuery" @showSizeChange="ajaxQuery"
                :show-total="(total) => `共${total}条`" />
        </div>
    </a-modal>
</template>
<script setup>
import { reactive, ref, onMounted } from 'vue';
import { message, Modal } from 'ant-design-vue';
import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
import { useRouter } from 'vue-router';
import { smartSentry } from '/@/lib/smart-sentry';
import { applicationManagementApi } from '/@/api/system/applicationManagement-api'
import {rolesApi} from '/@/api/access/roles'
import {rolemembersApi} from '/@/api/access/rolemembers'

let roleId =''
const visible = ref(false)
function showModal(id) {
    queryForm.roleId = id
    visible.value = true
    ajaxQuery();
}

defineExpose({
    showModal,
});
const props = defineProps({
    value: [Number, Array],
  });

  const emit = defineEmits(['update:value'],'change');
// --------------------------- 企业表格 列 ---------------------------

const columns = ref([
    {
        title: '序号',
        dataIndex: 'index',
        width: 50,
        align: 'center'
    },
    {
        title: '登录账号',
        dataIndex: 'username',
    },
    {
        title: '姓名',
        dataIndex: 'displayName',
    },
    {
        title: '部门名称',
        dataIndex: 'department',
    },
    {
        title: '职位',
        dataIndex: 'postName',
    },
    {
        title: '性别',
        dataIndex: 'gender',
    }
    
]);


// --------------------------- 查询 ---------------------------

const queryFormState = {
    pageNumber: 1,
    pageSize: PAGE_SIZE,
    // searchCount: true,
    username: '',
    roleId: roleId
};
const queryForm = reactive({ ...queryFormState });
const tableLoading = ref(false);
const tableData = ref([]);
const total = ref(0);


// 查询
function onSearch() {
    queryForm.pageNumber = 1;
    ajaxQuery();
}
// 重置查询
function resetQuery() {
    Object.assign(queryForm, queryFormState);
    ajaxQuery();
}
function dateToSeconds(dateStr) {
    if (dateStr == "") {
        return ""
    }
    let date = new Date(dateStr);
    return date.getTime() / 1000;
}
// 列表数据获取
async function ajaxQuery() {
    try {
        tableLoading.value = true;
        let responseModel = await rolemembersApi.getUser(queryForm);
        const list = responseModel.data.rows
        total.value = responseModel.data.records;
        tableData.value = list;
    } catch (e) {
        smartSentry.captureError(e);
    } finally {
        tableLoading.value = false;
    }
}

// 获取分类下拉
const FindList = ref([])
async function getFindList() {
    try {
        let res = await applicationManagementApi.queryFindList();
        FindList.value = res.data.map(item => {
            return {
                label: item.name,
                value: item.id
            }
        })
        FindList.value.unshift({ label: 'ALL', value: '' })
    } catch (e) {
        smartSentry.captureError(e);
    } finally {
    }
}
// 选择表格行
const selectedRowKeyList = ref([]);
const records = ref([])
function onSelectChange(selectedRowKeys,record) {
    selectedRowKeyList.value = selectedRowKeys;
    records.value = record
}
function tableSelectChange(){
    emit('update:value', selectedRowKeyList.value);
    emit('change',records.value);
    visible.value = false;
}
onMounted(() => {
    ajaxQuery();
    getFindList();
});
</script>
<style scoped></style>
